package net.mcreator.servercmd.procedure;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.World;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.scoreboard.ScoreCriteria;
import net.minecraft.scoreboard.Score;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.Entity;

import net.mcreator.servercmd.ElementsServerCmdMod;

import java.util.Map;

@ElementsServerCmdMod.ModElement.Tag
public class ProcedureDieBackSave extends ElementsServerCmdMod.ModElement {
	public ProcedureDieBackSave(ElementsServerCmdMod instance) {
		super(instance, 4);
	}

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			System.err.println("Failed to load dependency entity for procedure DieBackSave!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		if ((entity instanceof EntityPlayer)) {
			if (entity instanceof EntityPlayer) {
				Scoreboard _sc = ((EntityPlayer) entity).getWorldScoreboard();
				ScoreObjective _so = _sc.getObjective((entity.getEntityData().getString("TpaPlayerName")));
				if (_so == null) {
					_so = _sc.addScoreObjective((entity.getEntityData().getString("TpaPlayerName")), ScoreCriteria.DUMMY);
				}
				Score _scr = _sc.getOrCreateScore(((EntityPlayer) entity).getGameProfile().getName(), _so);
				_scr.setScorePoints((int) 0);
			}
			if (entity instanceof EntityPlayer) {
				Scoreboard _sc = ((EntityPlayer) entity).getWorldScoreboard();
				ScoreObjective _so = _sc.getObjective("BackWorld");
				if (_so == null) {
					_so = _sc.addScoreObjective("BackWorld", ScoreCriteria.DUMMY);
				}
				Score _scr = _sc.getOrCreateScore(((EntityPlayer) entity).getGameProfile().getName(), _so);
				_scr.setScorePoints((int) new Object() {
					int convert(String s) {
						try {
							return Integer.parseInt(s.trim());
						} catch (Exception e) {
						}
						return 0;
					}
				}.convert(("" + ((entity.dimension)))));
			}
			if (entity instanceof EntityPlayer) {
				Scoreboard _sc = ((EntityPlayer) entity).getWorldScoreboard();
				ScoreObjective _so = _sc.getObjective("BackX");
				if (_so == null) {
					_so = _sc.addScoreObjective("BackX", ScoreCriteria.DUMMY);
				}
				Score _scr = _sc.getOrCreateScore(((EntityPlayer) entity).getGameProfile().getName(), _so);
				_scr.setScorePoints((int) (entity.posX));
			}
			if (entity instanceof EntityPlayer) {
				Scoreboard _sc = ((EntityPlayer) entity).getWorldScoreboard();
				ScoreObjective _so = _sc.getObjective("BackY");
				if (_so == null) {
					_so = _sc.addScoreObjective("BackY", ScoreCriteria.DUMMY);
				}
				Score _scr = _sc.getOrCreateScore(((EntityPlayer) entity).getGameProfile().getName(), _so);
				_scr.setScorePoints((int) (entity.posY));
			}
			if (entity instanceof EntityPlayer) {
				Scoreboard _sc = ((EntityPlayer) entity).getWorldScoreboard();
				ScoreObjective _so = _sc.getObjective("BackZ");
				if (_so == null) {
					_so = _sc.addScoreObjective("BackZ", ScoreCriteria.DUMMY);
				}
				Score _scr = _sc.getOrCreateScore(((EntityPlayer) entity).getGameProfile().getName(), _so);
				_scr.setScorePoints((int) (entity.posZ));
			}
			if (entity instanceof EntityPlayer && !entity.world.isRemote) {
				((EntityPlayer) entity).sendStatusMessage(
						new TextComponentString((new TextComponentTranslation("msg.playerdie").getUnformattedComponentText())), (false));
			}
		}
	}

	@SubscribeEvent
	public void onEntityDeath(LivingDeathEvent event) {
		if (event != null && event.getEntity() != null) {
			Entity entity = event.getEntity();
			int i = (int) entity.posX;
			int j = (int) entity.posY;
			int k = (int) entity.posZ;
			World world = entity.world;
			java.util.HashMap<String, Object> dependencies = new java.util.HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			this.executeProcedure(dependencies);
		}
	}

	@Override
	public void preInit(FMLPreInitializationEvent event) {
		MinecraftForge.EVENT_BUS.register(this);
	}
}
